﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AceMobile.Models.Accessories;
using AceMobile.Models;

namespace AceMobile.Controllers.Admin
{
    [Authorize(Roles = "Administrator,Accessorymanager")]
    public class AccessoryAdminController : Controller
    {
        private MobileWebContext db = new MobileWebContext();

        //
        // GET: /AccessoryAdmin/

        public ViewResult Index()
        {
            var accessories = db.Accessories.Include(a => a.AccessoryProducer);
            return View(accessories.ToList());
        }

        //
        // GET: /AccessoryAdmin/Details/5

        public ViewResult Details(int id)
        {
            Accessory accessory = db.Accessories.Find(id);
            return View(accessory);
        }

        //
        // GET: /AccessoryAdmin/Create

        public ActionResult Create()
        {
            ViewBag.AccessoryProducerID = new SelectList(db.AccessoryProducers, "ID", "Name");
            return View();
        } 

        //
        // POST: /AccessoryAdmin/Create

        [HttpPost]
        public ActionResult Create(Accessory accessory)
        {
            if (ModelState.IsValid)
            {
                db.Accessories.Add(accessory);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.AccessoryProducerID = new SelectList(db.AccessoryProducers, "ID", "Name", accessory.AccessoryProducerID);
            return View(accessory);
        }
        
        //
        // GET: /AccessoryAdmin/Edit/5
 
        public ActionResult Edit(int id)
        {
            Accessory accessory = db.Accessories.Find(id);
            ViewBag.AccessoryProducerID = new SelectList(db.AccessoryProducers, "ID", "Name", accessory.AccessoryProducerID);
            return View(accessory);
        }

        //
        // POST: /AccessoryAdmin/Edit/5

        [HttpPost]
        public ActionResult Edit(Accessory accessory)
        {
            if (ModelState.IsValid)
            {
                db.Entry(accessory).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AccessoryProducerID = new SelectList(db.AccessoryProducers, "ID", "Name", accessory.AccessoryProducerID);
            return View(accessory);
        }

        //
        // GET: /AccessoryAdmin/Delete/5
 
        public ActionResult Delete(int id)
        {
            Accessory accessory = db.Accessories.Find(id);
            return View(accessory);
        }

        //
        // POST: /AccessoryAdmin/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Accessory accessory = db.Accessories.Find(id);
            db.Accessories.Remove(accessory);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}